package test.enba.jwt.controller;

import com.enba.boot.jwt.JwtHelper;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/jwt")
public class JwtController {

  @Autowired private JwtHelper jwtHelper;

  // 生成令牌
  @GetMapping("/createToken")
  public String createToken() {
    HashMap<String, Object> map = new HashMap<>();
    map.put("name", "张三");
    map.put("age", 28);
    map.put("flag", true);
    return jwtHelper.createToken(map);
  }

  // 解析令牌
  @PostMapping("/parseToken")
  public String parseToken(@RequestBody String token) {
    return jwtHelper.parseToken(token).toString();
  }

  // 校验令牌合法性
  @PostMapping("/validateToken")
  public boolean validateToken(@RequestBody String token) {
    return jwtHelper.validateToken(token);
  }
}
